//
//  HMVisitorView.swift
//  SecondWB
//
//  Created by 赖建伟 on 16/11/27.
//  Copyright © 2016年 Itheima. All rights reserved.
//

import UIKit

protocol HMVisitorViewDelegate:NSObjectProtocol {
    
    func registButtonDidClick()
    func loginButtonDidClick()
    
}

class HMVisitorView: UIView {
    
    weak var delegate: HMVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    //MARK: - 给外界赋值的方法
    func setInfo(image: String,message: String ,isHome: Bool = false) {
        
        normalImageView.image = UIImage(named:image)
        messageLabel.text = message
        
        if isHome {
            
            turnImageView.isHidden = false
            startAnimation()
            
            
        }else {
            
           turnImageView.isHidden = true
            
        }
        
    }
    //动画
    private func startAnimation() {
        
        let baseAnim = CABasicAnimation(keyPath: "transform.rotation")
        
        baseAnim.toValue = 2*M_PI
        baseAnim.duration = 5.0
        baseAnim.repeatCount = MAXFLOAT
        
        baseAnim.isRemovedOnCompletion = false
        
        turnImageView.layer.add(baseAnim, forKey: nil)
    }
    //MARK: - 添加视图并添加约束,点击事件
    private func setupUI() {
        backgroundColor = UIColor.white
        
        addSubview(normalImageView)
        addSubview(turnImageView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        normalImageView.snp_makeConstraints { (make) in
            make.center.equalTo(snp_center)
        }
        turnImageView.snp_makeConstraints { (make) in
            make.center.equalTo(snp_center)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(turnImageView.snp_bottom).offset(margin)
            make.centerX.equalTo(snp_centerX)
            make.width.equalTo(220)
        }
        registButton.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(margin)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        loginButton.snp_makeConstraints { (make) in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(margin)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        registButton.addTarget(self, action: #selector(registButtonClick), for: UIControlEvents.touchUpInside)
        loginButton.addTarget(self, action: #selector(loginButtonClick), for: UIControlEvents.touchUpInside)
        
    }
    
    //MARK:按钮点击
    @objc private func registButtonClick() {
        
        delegate?.registButtonDidClick()
    }
    @objc private func loginButtonClick() {
        
        delegate?.loginButtonDidClick()
    }
    //MARK: - 懒加载
    lazy var normalImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    lazy var turnImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    lazy var messageLabel = UILabel(title: "3333333", titleColor: UIColor.lightGray, font: 14, corner: 0, numbleOfLine: 0, textsAlignment: NSTextAlignment.center)
    
    lazy var registButton = UIButton(title: "注册", titleColor: UIColor.orange, titleFont: 14, normalImage: nil, backGroundImage: "common_button_white_disable", corner: 5)
    lazy var loginButton = UIButton(title: "登陆", titleColor: UIColor.orange, titleFont: 14, normalImage: nil, backGroundImage: "common_button_white_disable", corner: 5)
    
}
